<template>
	<view class="content">
		<view class="form-block" style="border-radius:30rpx 30rpx 0 0 ;">
			<view class="form-title">{{form.comName}}</view>
			<view class="form-label">
				<view class="form-title-label" v-if="form.isStateOwned>0">
					国有</view>
				<view class="form-title-label" v-if="form.isDomesticInvestment>0">
					内资</view>
				<view class="form-title-label" v-if="form.isGoOnTheMarket>0">
					上市</view>
			</view>
			<view class="sub-title">向求职者介绍一下公司吧</view>
			<view class="form-item photo">
				<view class="form-label">Logo(点击右边图片更换)</view>
				<image @click="chooseImage"
					:src="form.logoUrl?form.logoUrl:imageUrl+'/job_hunting/job_detail/enterprise.jpg'" class="photo">
				</image>
			</view>
			<view class="form-item">
				<view class="form-label">公司简称</view>
				<view class="form-content">
					<input v-model="form.brandName" maxlength="30" type="text" placeholder-class="inputPlaceholder"
						placeholder="请填写公司简称" class="form-ctrl" />

				</view>
			</view>
			<view class="form-item">
				<view class="form-label">所在街道/乡镇</view>
				<view class="form-content">
					<uni-section title="街道社区" type="line" padding class="form-ctrl" v-if="form.streetId==null">
						<uni-data-picker placeholder="请选择街道" popup-title="请选择社区" :localdata="list" :map="listMap"
							v-model="form.streetId">
						</uni-data-picker>
					</uni-section>
					<uni-section title="街道社区" type="line" padding class="form-ctrl" v-else>
						<uni-data-picker placeholder="请选择街道" popup-title="请选择社区" :localdata="list" :map="listMap"
							v-model="form.streetId" readonly="ture">
						</uni-data-picker>
					</uni-section>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">公司地址</view>
				<view class="form-content">
					<input v-model="form.enterpriseAddress" type="text" disabled="true" @click="chosenPosition"
						placeholder-class="inputPlaceholder" placeholder="请选择公司地址" class="form-ctrl" />
					<view class="address iconfont icon-dizhi"></view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">行业</view>
				<view class="form-content">
					<input v-model="form.comTypeText" style="color: gray;" maxlength="30" type="text"
						placeholder-class="inputPlaceholder" disabled class="form-ctrl" />
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">注册资本(万元)</view>
				<view class="form-content">
					<input v-model="form.regcap" type="number" placeholder-class="inputPlaceholder" disabled
						placeholder="请填写注册资本,单位为万元" class="form-ctrl" />

				</view>
			</view>
			<view class="form-item">
				<view class="form-label">人员规模</view>
				<view class="form-content">
					<picker :class="form.scaleId==-1 ? 'form-ctrl form-item-default':'form-ctrl'" :range="scaleList"
						range-key="data" :value="form.scaleId" @change="bindScale">
						{{form.scaleId==-1 ? "请选择人员规模" : scaleList[form.scaleId]['data']}}
					</picker>
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>

			<view class="form-item">
				<view class="form-label">公司简介</view>
				<view class="form-content">
					<textarea v-model="form.comIntroduction" class="form-ctrl" style="line-height:1.2;padding: 20rpx 0;"
						auto-height="true" maxlength="-1" placeholder="请填写公司简介"></textarea>
				</view>
			</view>
			<view @click="save" class="btn">{{btnText}}</view>
		</view>
	</view>
</template>

<script>
	import {
		validate
	} from '@/util/validate.js'
	import {
		getComInfo,
		getIndustry,
		getFinancing,
		getScale,
		submitCompanyOtherInfo
	} from '@/api/company.js'
	import {
		getQuestionnaireConfigCus,
	} from '@/api/registration.js'
	import {
		getStorage
	} from '@/util/auth.js';
	import {
		uploadFile
	} from '@/api/file.js'
	import {
		getUser
	} from '@/api/user.js'
	export default {
		data() {
			return {
				list: [],
				listMap: {
					text: 'name',
					value: 'id',
					children: 'child'
				},
				form: {
					logoUrl: '',
					comName: '',
					brandName: '',
					comType: '',
					scaleId: -1,
					comIntroduction: '',
					enterpriseAddress: '',
					enterpriseLongitude: '',
					enterpriseLatitude: '',
					regcap: '',
					isStateOwned: 0,
					isDomesticInvestment: 0,
					isGoOnTheMarket: 0,
					streetId: 0,
					comTypeText: ''
				},
				industryList: [],
				scaleList: [],

				rules: {

					brandName: {
						name: '品牌名称',
						required: true,
					},

					scaleId: {
						name: '人员规模',
						required: true,
						validator: function(value, form, callback) {
							if (value < 0) {
								callback('请选择人员规模')
							}
							callback();
						}
					},
					streetId: {
						name: '所在街道',
						required: true,
						validator: function(value, form, callback) {
							if (value < 0) {
								callback('请选择所在街道')
							}
							callback();
						}
					},
					enterpriseLongitude: {
						name: '公司地址',
						required: true
					},
					enterpriseLatitude: {
						name: '公司地址',
						required: true
					},
					enterpriseAddress: {
						name: '公司地址',
						required: true
					},

					comIntroduction: {
						name: '公司简介',
						required: true,
					},
				},

				type: null,
				imageUrl: '',
				pageType: '',
				btnText: ''
			}
		},
		methods: {
			chosenPosition() {

				uni.getLocation({
					isHighAccuracy: true,
					success: (current) => {
						const currentLatitude = current.latitude;
						const currentLongitude = current.longitude;
						uni.chooseLocation({
							latitude: currentLatitude,
							longitude: currentLongitude,
							success: (choosen) => {
								this.form.enterpriseAddress = choosen.address;
								this.form.enterpriseLatitude = choosen.latitude;
								this.form.enterpriseLongitude = choosen.longitude;
							},
							fail: (err) => {
								console.log(err);
								this.showToast('选择位置失败，请重试！');
							}
						})
					},
					fail: (e) => {
						this.showToast('获取位置失败，请重试！');
					}
				})
			},
			chooseImage() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album'],
					success: (e) => {
						uni.showLoading({
							title: '上传中',
							mask: true
						})
						uploadFile(e.tempFilePaths[0]).then((res) => {
							const data = JSON.parse(res.data);
							console.log(data)
							uni.hideLoading()
							if (data.code == 200) {
								this.showToast('上传成功')
								this.form.logoUrl = data.data

							} else {
								this.showToast(data.msg)
							}
						}).catch((res) => {
							uni.hideLoading()
							this.uls.showToast('上传失败');
						})
					}
				})
			},

			bindIndustry(e) {
				this.form.comType = e.detail.value
			},
			bindScale(e) {
				this.form.scaleId = e.detail.value
			},
			getIndustryList(resolve, reject) {
				getIndustry({
					postType: 2
				}).then((res) => {
					this.industryList = res;
					resolve();
				}).catch((res) => {
					reject(res);
				})
			},
			getScaleList(resolve, reject) {
				getScale().then((res) => {
					this.scaleList = res;
					resolve()
				}).catch((res) => {
					reject(res)
				})
			},
			getList(resolve, reject) {
				getQuestionnaireConfigCus({
					type: 4
				}).then((res) => {
					this.list = res ?? [];
					resolve();
				}).catch((res) => {
					reject(res);
				})
			},
			getCompanyInfo(resolve, reject) {
				getComInfo().then((res) => {
					//有企业用户已注册企业信息，此处回显修改
					const info = res;
					this.form.brandName = info.brandName;
					this.form.comIntroduction = info.comIntroduction;
					this.form.logoUrl = info.logoUrl ?? '';
					this.form.regcap = info.regcap;
					if (info.enterpriseLongitude) {
						this.form.enterpriseAddress = info.enterpriseAddress;
					}
					this.form.enterpriseLongitude = info.enterpriseLongitude;
					this.form.enterpriseLatitude = info.enterpriseLatitude;
					this.form.comName = info.comName;
					this.form.comType = info.comType
					this.form.comTypeText = info.comTypeText
					this.form.isStateOwned = info.isStateOwned
					this.form.isDomesticInvestment = info.isDomesticInvestment
					this.form.isGoOnTheMarket = info.isGoOnTheMarket
					this.form.streetId = info.streetId
					this.scaleList.forEach((item, index) => {
						if (item.id == info.scaleId) {
							this.form.scaleId = index;
							return;
						}
					})
					resolve()

				}).catch((res) => {
					reject(res)
				})
			},

			save() {
				const form = JSON.parse(JSON.stringify(this.form));
				form.type = 'modify';
				if (validate(form, this.rules)) {
					uni.showLoading({
						title: '提交中',
						mask: true
					})
					form.scaleId = this.scaleList[this.form.scaleId].id;
					submitCompanyOtherInfo(form).then((res) => {
						uni.hideLoading()
						this.showToast('保存成功', function() {
							uni.switchTab({
								url: '/pages/mine/index'
							})
						})
					}).catch((res) => {
						uni.hideLoading()
					})
				}
			}
		},
		onLoad(e) {
			this.imageUrl = this.globalConfig.imageUrl;
			this.btnText = '保存'
			this.pageType = '编辑'

			uni.showLoading({
				title: '加载中',
				mask: true
			})
			let promise1 = new Promise((resolve, reject) => {
				this.getIndustryList(resolve, reject); //行业
			})
			let promise2 = new Promise((resolve, reject) => {
				this.getScaleList(resolve, reject); //人员规模
			})
			let promise3 = new Promise((resolve, reject) => {
				this.getCompanyInfo(resolve, reject);
			})
			let promise4 = new Promise((resolve, reject) => {
				this.getList(resolve, reject); //企业社区
			})
			Promise.all([promise1, promise2, promise3, promise4]).then((res) => {
				console.log()
				uni.hideLoading()
			}).catch((res) => {
				uni.hideLoading()
				this.showToast('加载失败，请重试', () => {
					uni.navigateBack()
				})
			})
		},


	}
</script>

<style lang="scss">
	.content {
		width: 100%;

		.btn {
			width: 350rpx;
			height: 80rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: #FF7626;
			color: #fff;
			font-size: 28rpx;
			border-radius: 40rpx;
			margin: 0 auto;
			margin-top: 30rpx;
		}

		.user-photo-block {
			width: 100%;
			height: 450rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.user-photo {
				width: 180rpx;
				height: 180rpx;
				border-radius: 50%;
			}
		}

		.form-block {
			width: 100%;
			background: #fff;
			padding: 30rpx;

			.form-title {
				font-size: 36rpx;
				font-weight: bold;
			}

			.form-label {
				display: flex;
				margin-top: 20rpx;

				.form-title-label {
					font-size: 12px;
					color: #0256FF;
					background-color: #CCDDFF;
					width: 120rpx;
					height: 40rpx;
					border-radius: 10rpx;
					text-align: center;
					padding-top: 3rpx;
					margin-right: 20rpx;
				}
			}

			.sub-title {
				margin: 20rpx 0;
				font-size: 26rpx;
				color: #999;
			}

			.form-item {
				display: flex;
				flex-direction: column;
				border-bottom: 1px solid #efefef;
				padding-top: 30rpx;

				.form-label {
					font-size: 28rpx;
					color: #999;
				}

				.form-content {
					display: flex;
					flex-direction: row;
					align-items: center;

					.form-ctrl {
						height: 100rpx;
						line-height: 100rpx;
						flex-grow: 1;
					}

					.form-item-default {
						color: #999;
					}

					.time-divide {
						padding: 0 10rpx;
					}

					.arrow {
						flex-grow: 0;
						color: #999;
						font-size: 24rpx;
					}
				}
			}

			.form-item.photo {
				display: flex;
				flex-direction: row;
				align-items: center;
				padding-bottom: 30rpx;

				.photo {
					flex-grow: 0;
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
					background: #999;
				}

				.form-label {
					flex-grow: 1;
				}
			}
		}
	}
</style>